const app = getApp();
var interval = null //倒计时函数
Page({
  data:{
    time: '获取验证码',
    currentTime: 61
  },
  getphone:function(e){
    this.setData({
      phone:e.detail.value
    })
    console.log(this.data.phone)
  },
  sendCode:function(){
    const _this = this;
    // console.log(_this.data.phone);
    // console.log(_this.data.phone.length);
    if (_this.data.phone && _this.data.phone.length == 11){
      _this.settimeD();
      _this.setData({
        disabled: true
      })
      wx.request({
        url: app.globalData.path + 'services/app/applets/SendSMSCode',
        data: {
          phoneNumber: _this.data.phone,
          SendType: 2
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.success) {
            _this.setData({
              securityCode: res.data.result.securityCode
            })
            if (res.data.result.sendResultType == 2) {
              wx.showToast({
                title: '手机号不存在',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 3) {
              wx.showToast({
                title: '操作频繁',
                icon: 'none',
                duration: 2000
              })
            } else if (res.data.result.sendResultType == 4 || res.data.result.sendResultType == 5) {
              wx.showToast({
                title: '短信模板错误',
                icon: 'none',
                duration: 2000
              })
            }
          } else {
            wx.showToast({
              title: '发送失败，请重试',
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }else{
      wx.showToast({
        title: '请输入正确格式的手机号',
        icon: 'none',
        duration: 2000
      })
    }
  },
  formSubmit:function(e){
    console.log(e.detail.value.phone);
    console.log(e.detail.value.pwd);
    console.log(e.detail.value.testNumber);
    if(e.detail.value.phone == ''){
      wx.showModal({
        content: '请填写手机号',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      });
    } else if (e.detail.value.pwd == '') {
      wx.showModal({
        content: '请输入密码',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      });
    }else if(e.detail.value.pwd.length >= 6 && e.detail.value.pwd.length <=18){
      wx.request({
        url: app.globalData.path + 'services/app/applets/ResetPassword',
        data: {
          phoneNumber: e.detail.value.phone,
          password: e.detail.value.pwd,
          code: e.detail.value.testNumber,
          securityCode: this.data.securityCode
        },
        method: 'POST',
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          // console.log(res.data);
          if (res.data.success) {
            wx.showToast({
              title:'修改成功',
              icon: 'success',
              duration: 2000
            })
            wx.navigateTo({
              url: '../login/login'
            })
          } else {
            wx.showToast({
              title: res.data.error.message,
              icon: 'none',
              duration: 2000
            })
          }
        },
        fail: function (res) {
          console.log(res.error.message);
        }
      })
    }else{
      wx.showModal({
        content: '密码格式不对，要求6~18位',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      });
    }
  },
  //倒计时
  settimeD: function (options) {
    var that = this;
    var currentTime = that.data.currentTime
    interval = setInterval(function () {
      currentTime--;
      that.setData({
        time: currentTime + '秒'
      })
      if (currentTime <= 0) {
        clearInterval(interval)
        that.setData({
          time: '重新发送',
          currentTime: 61,
          disabled: false
        })
      }
    }, 1000)
  },
})